package com.zhss.designpattern.iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fanwei
 * @desc 设计模式-- 迭代器模式实现
 */
public class IteratorPatternDemo {
    //这是一个测试时候用的类
    static class Student{
        private String  name;
        private Integer age;

        public Student(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

    /**
     * 定义一个我们自己的迭代器接口
     * @author fanwei
     */
    public interface Iterator{
        public Object next();//获取下一个方法
        public boolean hasNext();//是否有下一个方法
    }

    /**
     * 定义一个容器接口
     */
    public interface Aggregate{
        public java.util.Iterator itrator();//遍历迭代器的方法
        public void add(Object obj);
        public void remove(Object obj);
    }

    /**
     * 定义一个容器实现类
     */
    static class ConcreteAggregate implements Aggregate{
        List students;
        Integer last = 0;

        public ConcreteAggregate(Integer size) {
            this.students = new ArrayList(size);
        }

        public java.util.Iterator itrator() {
            return this.students.iterator();
        }

        public void add(Object obj) {
            students.add(obj);
            last++;
        }

        public Object getObj(Integer index){
            return students.get(index);
        }

        public void remove(Object obj) {
           students.remove(obj);
           last--;
        }

        public Integer getSize(){
            return last;
        }

    }

    /**
     * 迭代器实现类
     */
    class ConcreteIterator implements Iterator{

        private ConcreteAggregate concreteAggregate;
        private Integer index;

        public ConcreteIterator(ConcreteAggregate concreteAggregate, Integer index) {
            this.concreteAggregate = concreteAggregate;
            this.index = 0;
        }

        public Object next() {
            Object obj = concreteAggregate.getObj(index);
            index++;
            return obj;
        }

        public boolean hasNext() {
            if (index < concreteAggregate.getSize()){
                return true;
            }else {
                return false;
            }
        }
    }

    public static void main(String[] args) {
        Student student1 = new Student("a",12);
        Student student2 = new Student("b",6);

        ConcreteAggregate concreteAggregate = new ConcreteAggregate(2);
        concreteAggregate.add(student1);
        concreteAggregate.add(student2);

        java.util.Iterator iterator = concreteAggregate.itrator();
        while (iterator.hasNext()){
           Student student = (Student) iterator.next();
            System.out.println(student);
        }
    }
}
